<?php

/**
 * 后台用户相关model
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;
use backend\models\Admin;
use backend\models\novel\Novel;
use backend\models\Recharge;

class User extends ActiveRecord
{
    /**
     * 获取用户信息
     */
    public function getUserInfo( $uid )
    {
        $user = self::find();
        $user->select('*');
        $user->where(array('user_id' => $uid));
        $user->andWhere(array('user_status'=>'1'));
        $result = $user->asArray()->one();
        return $result;
    }
    
    /**
     * 获取用户列表
     */
    public function getList( $params = array() )
    {
        $user = self::find();
        $user->select('*');
        $page = $params['page'];
        $offset = 0;
        $limit = $params['pageSize'];
        if($page > 0){
            $offset = ($page - 1) * $limit;
        }
        if($params['search'] != ''){
            $user->where('user_nickname like :search');
            $user->addParams(array(':search'=>'%'.$params['search'].'%'));
        }
        $count = $user->count();
        $user->offset( $offset )->limit( $limit );
        $result = $user->asArray()->all();
        if($result){
            //合作方、渠道
            $pids = array_column( $result , 'partner_id' );
            $cids = array_column( $result , 'channel_id' );
            $admin = new Admin();
            $param['ids'] = array_unique( array_merge_recursive( $pids , $cids ) );
            $admins = $admin->getAdminList( $param );
            $a_k_v = array_column( $admins['list'] , null , 'id' );
            unset($admins);
            //小说来源
            $novel_id = array_column( $result , 'user_reg_novel_id' );
            $novel = new Novel();
            $novels = $novel->getNovelList( array( 'ids' => array_unique( $novel_id ) ) );
            $n_k_v = array_column( $novels['list'] , null , 'novel_id' );
            unset($novels);
            //消费记录
            $recharge = new Recharge();
            $chs = $recharge->getUserTotal( array( 'uids' => array_column( $result , 'user_id' ) ) );
            $chs_k_v = array_column( $chs , null , 'user_id' );
            unset($chs);
            foreach($result as $k=>$v){
                $result[$k]['partner'] = isset($a_k_v[$v['partner_id']]) ? $a_k_v[$v['partner_id']]['username'] : '无';
                $result[$k]['channel'] = isset($a_k_v[$v['channel_id']]) ? $a_k_v[$v['channel_id']]['username'] : '无';
                $result[$k]['novel']   = isset($n_k_v[$v['user_reg_novel_id']]) ? $n_k_v[$v['user_reg_novel_id']]['novel_title'] : '无';
                $result[$k]['pay_money']   = isset($chs_k_v[$v['user_id']]) ? $chs_k_v[$v['user_id']]['money'] : '暂未充值';
                $result[$k]['pay_cnt']   = isset($chs_k_v[$v['user_id']]) ? $chs_k_v[$v['user_id']]['cnt'] : '0';
            }
        }
        $data['dataProvider'] = new ArrayDataProvider(array(
            'allModels'=> $result,
//            'sort' => [
//                'attributes' => ['userId', 'createTime', 'sex', 'nickname','lastTime'],
//            ],
        ));
        $data['pagination'] = new Pagination(array(
//            'pageSize'          => $limit,
            'defaultPageSize'   => $limit,
            'totalCount'        => $count,
        ));
        return $data;
    }
    
    /**
     * 封号
     */
    public function disabledUser( $uid = 0 ) {
        if( !$uid ){
            return false;
        }
        $user = self::findOne($uid);
        $user->user_status = 2;
        $res = $user->save();
        return $res;
    }
    
    /**
     * 解封
     */
    public function undisabledUser( $uid = 0 ) {
        if( !$uid ){
            return false;
        }
        $user = self::findOne($uid);
        $user->user_status = 1;
        $res = $user->save();
        return $res;
    }
    
    
    public static function getUsers($params = array())
    {
        $model = self::find();
        if(isset($params['tweet'])){
            $model->andWhere(['user_reg_recommend_sn'=>$params['tweet']]);
        }
        $data = $model->asArray()->all();
        return $data;
    }
}

